package com.suep.game_chat.controller;

import com.suep.game_chat.entity.BrowseHistory;
import com.suep.game_chat.entity.ContentFilter;
import com.suep.game_chat.entity.UserPreference;
import com.suep.game_chat.service.BehaviorService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户行为控制器
 * 处理用户浏览历史、偏好设置、内容过滤和推荐等功能
 *
 * @author Game Chat Team
 * @version 1.0
 */
@RestController
@RequestMapping("/userapi/behavior")
@RequiredArgsConstructor
public class BehaviorController {

    private final BehaviorService behaviorService;

    /**
     * 记录用户浏览历史
     *
     * @param history 浏览历史记录
     * @return ResponseEntity 操作结果
     */
    @PostMapping("/history")
    public ResponseEntity<Void> recordBrowseHistory(@RequestBody BrowseHistory history) {
        behaviorService.recordBrowseHistory(history);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取用户浏览历史
     *
     * @param userId 用户ID
     * @param page 页码，默认1
     * @param size 每页数量，默认20
     * @return ResponseEntity 包含浏览历史列表
     */
    @GetMapping("/history/{userId}")
    public ResponseEntity<List<BrowseHistory>> getBrowseHistory(
            @PathVariable String userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        List<BrowseHistory> history = behaviorService.getBrowseHistory(userId, page, size);
        return ResponseEntity.ok(history);
    }

    /**
     * 清空用户浏览历史
     *
     * @param userId 用户ID
     * @return ResponseEntity 操作结果
     */
    @DeleteMapping("/history/{userId}")
    public ResponseEntity<Void> clearBrowseHistory(@PathVariable String userId) {
        behaviorService.clearBrowseHistory(userId);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除指定的浏览历史记录
     *
     * @param historyId 历史记录ID
     * @return ResponseEntity 操作结果
     */
    @DeleteMapping("/history/item/{historyId}")
    public ResponseEntity<Void> deleteBrowseHistory(@PathVariable Long historyId) {
        behaviorService.deleteBrowseHistory(historyId);
        return ResponseEntity.ok().build();
    }

    /**
     * 保存用户偏好设置
     *
     * @param preference 用户偏好信息
     * @return ResponseEntity 操作结果
     */
    @PostMapping("/preference")
    public ResponseEntity<Void> saveUserPreference(@RequestBody UserPreference preference) {
        behaviorService.saveUserPreference(preference);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取用户偏好设置列表
     *
     * @param userId 用户ID
     * @return ResponseEntity 包含用户偏好设置列表
     */
    @GetMapping("/preference/{userId}")
    public ResponseEntity<List<UserPreference>> getUserPreferences(@PathVariable String userId) {
        List<UserPreference> preferences = behaviorService.getUserPreferences(userId);
        return ResponseEntity.ok(preferences);
    }

    /**
     * 更新用户偏好设置
     *
     * @param preference 更新的偏好设置信息
     * @return ResponseEntity 操作结果
     */
    @PutMapping("/preference")
    public ResponseEntity<Void> updateUserPreference(@RequestBody UserPreference preference) {
        behaviorService.updateUserPreference(preference);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除用户偏好设置
     *
     * @param preferenceId 偏好设置ID
     * @return ResponseEntity 操作结果
     */
    @DeleteMapping("/preference/{preferenceId}")
    public ResponseEntity<Void> deleteUserPreference(@PathVariable Long preferenceId) {
        behaviorService.deleteUserPreference(preferenceId);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加内容过滤规则
     *
     * @param filter 过滤规则信息
     * @return ResponseEntity 操作结果
     */
    @PostMapping("/filter")
    public ResponseEntity<Void> addContentFilter(@RequestBody ContentFilter filter) {
        behaviorService.addContentFilter(filter);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取用户的内容过滤规则
     *
     * @param userId 用户ID
     * @return ResponseEntity 包含过滤规则列表
     */
    @GetMapping("/filter/{userId}")
    public ResponseEntity<List<ContentFilter>> getContentFilters(@PathVariable String userId) {
        List<ContentFilter> filters = behaviorService.getContentFilters(userId);
        return ResponseEntity.ok(filters);
    }

    /**
     * 更新内容过滤规则
     *
     * @param filter 更新的过滤规则信息
     * @return ResponseEntity 操作结果
     */
    @PutMapping("/filter")
    public ResponseEntity<Void> updateContentFilter(@RequestBody ContentFilter filter) {
        behaviorService.updateContentFilter(filter);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除内容过滤规则
     *
     * @param filterId 过滤规则ID
     * @return ResponseEntity 操作结果
     */
    @DeleteMapping("/filter/{filterId}")
    public ResponseEntity<Void> deleteContentFilter(@PathVariable Long filterId) {
        behaviorService.deleteContentFilter(filterId);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取推荐内容
     *
     * @param userId 用户ID
     * @param page 页码，默认1
     * @param size 每页数量，默认20
     * @return ResponseEntity 包含推荐内容列表
     */
    @GetMapping("/recommend/{userId}")
    public ResponseEntity<List<String>> getRecommendedContent(
            @PathVariable String userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        List<String> recommendations = behaviorService.getRecommendedContent(userId, page, size);
        return ResponseEntity.ok(recommendations);
    }

    /**
     * 更新内容偏好反馈
     *
     * @param userId 用户ID
     * @param contentType 内容类型
     * @param contentId 内容ID
     * @param isPositive 是否是正面反馈
     * @return ResponseEntity 操作结果
     */
    @PostMapping("/feedback")
    public ResponseEntity<Void> updateContentPreference(
            @RequestParam String userId,
            @RequestParam String contentType,
            @RequestParam String contentId,
            @RequestParam boolean isPositive) {
        behaviorService.updateContentPreference(userId, contentType, contentId, isPositive);
        return ResponseEntity.ok().build();
    }
} 